package com.lbloca.locate.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.appcompat.widget.Toolbar;

import com.gyf.immersionbar.ImmersionBar;
import com.lbloca.locate.R;
import com.lbloca.locate.ui.base.BaseActivity;
import com.lbloca.locate.utils.GlideCacheUtil;
import com.lbloca.locate.utils.ToastUtils;
import com.lbloca.locate.weight.ConversationDialog;
import com.lbloca.locate.weight.LoadingDialog;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 设置
 */

public class SettingActivity extends BaseActivity {
    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_rl_about_us)
    RelativeLayout mRlAboutUs;
    @BindView(R.id.id_tv_cache)
    TextView mTvCache;
    @BindView(R.id.id_rl_clear_cache)
    RelativeLayout mClearCache;
    @BindView(R.id.id_rl_check_update)
    RelativeLayout mRlCheckUpdate;

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }

    @Override
    protected void initView() {
        mTvCache.setText(GlideCacheUtil.getInstance().getCacheSize(this));
    }

    @Override
    protected void initEvents() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, SettingActivity.class);
        context.startActivity(intent);
    }

    private LoadingDialog loadingDialog;

    @OnClick({R.id.id_rl_about_us, R.id.id_rl_clear_cache, R.id.id_rl_check_update, R.id.id_rl_privacystatement, R.id.id_rl_user_deal})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.id_rl_about_us:
                startActivity(new Intent(this, AboutUsActivity.class));
                break;
            case R.id.id_rl_clear_cache:
                final ConversationDialog conversationDialog = new ConversationDialog(this).builder();
                conversationDialog.setBtnText(R.string.clear);
                conversationDialog.setGravity(Gravity.CENTER);
                conversationDialog.settitlemessage(getResources().getString(R.string.clear_info));
                conversationDialog.setOnClickListener(new ConversationDialog.OnClickListener() {
                    @Override
                    public void clickRightBtn() {
                        loadingDialog = new LoadingDialog(SettingActivity.this);
                        loadingDialog.setText("清理中...");
                        loadingDialog.show();
                        conversationDialog.dismiss();
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                GlideCacheUtil.getInstance().clearImageAllCache(SettingActivity.this);
                                mHandler.sendEmptyMessageDelayed(1, 500);
                            }
                        }).start();
                    }

                    @Override
                    public void clickLeftBtn() {
                        conversationDialog.dismiss();
                    }
                });
                conversationDialog.show();
                break;
            case R.id.id_rl_check_update:
                loadingDialog = new LoadingDialog(this);
                loadingDialog.setText("检查中...");
                loadingDialog.show();
                mHandler.sendEmptyMessageDelayed(2, 1000);
                break;
            case R.id.id_rl_user_deal:
                Intent intent = new Intent(SettingActivity.this, PrivacyNotesActivity.class);
                intent.putExtra(PrivacyNotesActivity.ARG_TITLE, "用户协议");
                startActivity(intent);
                break;
            case R.id.id_rl_privacystatement:
                Intent intent1 = new Intent(SettingActivity.this, PrivacyNotesActivity.class);
                intent1.putExtra(PrivacyNotesActivity.ARG_TITLE, "隐私政策");
                startActivity(intent1);
                break;
            default:
                break;
        }
    }

    @SuppressWarnings("HandlerLeak")
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (loadingDialog != null) {
                loadingDialog.dismiss();
            }
            if (msg.what == 1) {
                mTvCache.setText(GlideCacheUtil.getInstance().getCacheSize(SettingActivity.this));
                ToastUtils.showShort("清理成功");
            } else {
                ToastUtils.showShort("已经是最新版本...");
            }
        }
    };

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .navigationBarDarkIcon(true)
                .titleBar(mToolbar)
                .init();
    }
}
